package com.zenika.zenbinding.client.handler;

import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.zenika.zenbinding.client.model.ValueModel;

public class TextBoxHandler extends ValueBindingHandler implements ChangeListener {

	private TextBox textBox;
	private ValueModel model;

	public TextBoxHandler(TextBox textBox, ValueModel model) {
		super();
		this.textBox = textBox;
		this.model = model;
		this.model.setHandler(this);
		this.bind();
	}

	private void bind() {
		this.textBox.setText(this.model.getValue());
		this.textBox.addChangeListener(this);
	}

	public void onChange(Widget sender) {
		this.fireChange(this.textBox.getText(), this);
	}

	public void setRegularStyle() {
		this.textBox.setStyleName("gwt-TextBox");
	}

	public void setErrorStyle() {
		this.textBox.setStyleName("error-TextBox");
	}

	public Widget getWidget() {
		return this.textBox;
	}

	public Object getValue() {
		return this.textBox.getText();
	}

}
